﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using System.IO;
using He.Common;
using System.Data;
using System.Text;
namespace He.Web.en
{
    public partial class index : Front
    {
        string theme;
        string templateDir;
        TemplateHelper th;
        protected void Page_Load(object sender, EventArgs e)
        {
            string templateName = siteInfo.Template;
            theme = string.Format("{0}templates/{1}/", webset.web_path, templateName);
            templateDir = PathHelper.MapPath(string.Format("~/templates/{0}/", templateName));

            this.th = new TemplateHelper(templateDir);

            PutCommon();  //加载公共部分

            DisplayPage();
        }

        //首页
        private void DisplayPage()
        {
            string page = InputUtil.Get("p", "index");
            switch (page)
            {
                case "page":
                    LoadPage();
                    break;
                case "list":
                    LoadList();
                    break;
                case "show":
                    LoadShow();
                    break;
                case "plug":
                    LoadPlug();
                    break;
                default:
                    LoadDefault();
                    break;
            }
        }

        #region 加载首页=====================================
        private void LoadDefault()
        {
            this.th.Put("title", siteInfo.WebTitle);
            th.Put("metainfo", AddMetaInfo("", ""));
            Display("index.html");
        }
        #endregion

        #region 加载内容页===================================
        private void LoadPage()
        {
            BLL.article bll = new BLL.article();
            BLL.channel cbll = new BLL.channel();
            BLL.category catebll = new BLL.category();
            string defaultName = "en_about";
            string name = InputUtil.Get("name", defaultName);
            int id = GetInt("id", bll.GetFirstId(name));
            Model.article model = bll.GetModel(id);
            Model.category category = new BLL.category().GetModel(model.category_id);
            Model.channel channel = cbll.GetModel(category.channel_id);
            Model.channel sideChannel = new Model.channel();
            if (name == channel.name)
            {
                sideChannel = channel;
            }
            else
            {
                sideChannel = cbll.GetModel(name);
            }
            LoadSide(sideChannel, defaultName);
            this.PutPrevNext(category.channel_id, id);
            th.Put("displayPart", HtmlHelper.ReplaceMyFocusEffect(channel.show_effect));
            th.Put("channel", channel);
            th.Put("category", category);
            th.Put("model", model);
            th.Put("title", WebTitle(model.title));
            th.Put("crumb_txt", model.title);
            th.Put("metainfo", AddMetaInfo(model.seo_keywords, StringHelper.CutString(model.seo_desc, 250)));
            Display("show.html");
        }
        #endregion

        #region 加载列表页===================================
        private void LoadList()
        {
            string defaultName = "en_product";
            string name = InputUtil.Get("name", defaultName);
            int category_id = GetInt("category_id", 0);
            int page = GetInt("page", 0);
            string keywords = GetString("keywords");

            BLL.article abll = new BLL.article();
            Model.channel channel = new BLL.channel().GetModel(name); //栏目
            LoadSide(channel, defaultName);

            string strWhere = CombSqlTxt(channel.id, category_id, keywords);
            List<Model.article> list = new List<Model.article>();
            list = abll.DataTableToList(abll.GetPageList(channel.page_size, page, strWhere, channel.order_by).Tables[0]);

            int rcount = abll.GetRecordCount(strWhere);
            string pager = HtmlHelper.OutPageListEn(channel.page_size, page, rcount, CombUrlTxt(name, category_id, keywords));//分页

            #region SEO信息
            string crumb_txt = channel.title;
            string webtitle = WebTitle(channel.title);
            string seo_keywords = channel.seo_keywords;
            string seo_desc = channel.seo_desc;
            if (category_id > 0)
            {
                Model.category cmodel = new BLL.category().GetModel(category_id);
                crumb_txt = cmodel.title;
                webtitle = WebTitle(cmodel.title);
                seo_keywords = cmodel.seo_keywords;
                seo_desc = cmodel.seo_desc;
            }
            #endregion

            th.Put("channel", channel);
            th.Put("list", list);
            th.Put("pager", pager);
            th.Put("crumb_txt", crumb_txt);
            th.Put("title", webtitle);
            th.Put("metainfo", AddMetaInfo(seo_keywords, seo_desc));
            Display("list.html");
        }
        #endregion

        #region 加载详细页===================================
        private void LoadShow()
        {
            int id = GetInt("id", 0);

            BLL.channel channelbll = new BLL.channel();
            BLL.category catebll = new BLL.category();
            BLL.article abll = new BLL.article();
            BLL.attr_value avbll = new BLL.attr_value();
            Model.channel channel = new Model.channel();
            Model.category category = new Model.category();
            Model.article model = new Model.article();

            abll.UpdateField(id, "click=click+1");
            model = abll.GetModel(id);
            model.attr_value = avbll.GetModelList("picture_id=" + model.id);
            category = catebll.GetModel(model.category_id);
            channel = channelbll.GetModel(category.channel_id);

            th.Put("title", WebTitle(category.title, model.title));
            th.Put("metainfo", AddMetaInfo(model.seo_keywords, model.seo_desc));
            th.Put("model", model);

            LoadSide(channel, "en_product");
            if (channel.show_comment == 1)
            {
                LoadComment(channel, model.id);
            }

            this.PutPrevNext(category.channel_id, id);

            th.Put("displayPart", HtmlHelper.ReplaceMyFocusEffect(channel.show_effect));
            th.Put("channel", channel);
            th.Put("category", category);
            th.Put("crumb_txt", category.title);

            Display("show.html");
        }
        #endregion

        #region 加载插件页===================================
        private void LoadPlug()
        {
            string act = InputUtil.Get("act");
            string name = InputUtil.Get("name", "en_about");
            string title = "";
            string crumb_txt = "";
            string crumb_ico = "";
            string plug = "";

            if (string.IsNullOrEmpty(act))
            {
                Response.End();
            }
            switch (act)
            {
                case "feedback": //意见反馈
                    title = "Feedback";
                    crumb_txt = title;
                    crumb_ico = "feedback";
                    plug = "ucfeedback";
                    LoadFeedback();
                    break;
                case "job": //人才招聘
                    title = "Recruitment";
                    crumb_txt = title;
                    crumb_ico = "job";
                    plug = "ucjob";
                    LoadJob();
                    break;
                case "jobdetail": //招聘详情
                    title = "Recruitment";
                    crumb_txt = title;
                    crumb_ico = "job";
                    plug = "ucjobdetail";
                    LoadJobDetail();
                    break;
                case "jobapply": //提交简历
                    title = "Recruitment";
                    crumb_txt = title;
                    crumb_ico = "job";
                    plug = "ucjobapply";
                    LoadJobApply();
                    break;
                case "order": //在线订单
                    title = "Online Order";
                    crumb_txt = title;
                    crumb_ico = "order";
                    plug = "ucorder";
                    LoadOrder();
                    break;
                case "network": //营销网络
                    title = "Marketing Network";
                    crumb_txt = title;
                    crumb_ico = "network";
                    plug = "ucnetwork";
                    break;
                case "links": //友情链接
                    title = "Links";
                    crumb_txt = title;
                    crumb_ico = "links";
                    plug = "uclinks";
                    LoadLinks();
                    break;
                case "logistics"://物流跟踪
                    title = "Logistics";
                    crumb_txt = title;
                    crumb_ico = "logistics";
                    plug = "uclogistics";
                    LoadLogistics();
                    break;
                default:
                    Response.End();
                    break;
            }

            Model.channel channel = new BLL.channel().GetModel(name);
            LoadSide(channel, name);

            th.Put("name", name);
            th.Put("channel", channel);
            th.Put("crumb_txt", crumb_txt);
            th.Put("crumb_ico", crumb_ico);
            th.Put("plug", plug);
            th.Put("title", WebTitle(title));
            th.Put("metainfo", AddMetaInfo("", ""));

            Display("plug.html");
        }
        #endregion

        //加载侧边栏分类
        private void LoadSide(Model.channel _channel, string _defaultName)
        {
            BLL.channel bll = new BLL.channel();
            Model.channel sideChannel = _channel;

            //如果不需要显示分类，则显示默认频道分类
            if (_channel.show_category == 0 && _channel.model_id != (int)Enums.sys_model.内容模块)
            {
                sideChannel = bll.GetModel(_defaultName);
            }

            StringBuilder strCategorys = new StringBuilder();
            string[] idArr;
            if (string.IsNullOrEmpty(sideChannel.bind_id))
            {
                idArr = new string[] { sideChannel.id.ToString() };
            }
            else
            {
                idArr = sideChannel.bind_id.Split(',');
            }
            foreach (string item in idArr)
            {
                int bind_id;
                if (int.TryParse(item, out bind_id))
                {
                    Model.channel bind_channel = new BLL.channel().GetModel(bind_id);
                    if (bind_channel.model_id == (int)Enums.sys_model.内容模块)
                    {
                        if (bind_channel.show_category == 0)
                        {
                            strCategorys.Append(HtmlHelper.GetContentList(bind_channel.name));
                        }
                        else
                        {
                            strCategorys.Append(HtmlHelper.GetCateAndArticleList(bind_channel.name));
                        }
                    }
                    else
                    {
                        strCategorys.Append(HtmlHelper.GetCategoryList(bind_channel.name));
                    }
                }
            }
            th.Put("category_id", InputUtil.Get("category_id", 0));
            th.Put("sideChannel", sideChannel);
            th.Put("sideCategorys", strCategorys.ToString());
        }
        //加载评论
        private void LoadComment(Model.channel _channel, int _pid)
        {
            string condition = "channel_id=" + _channel.id + " and pid=" + _pid;
            if (_channel.comment_audit == 1)
            {
                condition += " and is_lock=0";
            }
            int commentCount = new BLL.comment().GetRecordCount(condition);

            th.Put("commentCount", commentCount);
        }
        //加载意见反馈
        private void LoadFeedback()
        {
            int page_size = 5;
            int page = GetInt("page", 0);

            BLL.feedback bll = new BLL.feedback();
            DataTable list = new DataTable();
            string strWhere = string.Format("status={0} and pos_mark='feedback' and lang='en'", (int)Enums.AuditStatus.已审核);
            list = bll.GetPageList(page_size, page, strWhere, "id desc").Tables[0];

            int rcount = bll.GetRecordCount(strWhere);
            string pager = HtmlHelper.OutPageListEn(page_size, page, rcount, "index.aspx?p=plug&page=__id__&act=feedback");
            th.Put("list", list);
            th.Put("pager", pager);
        }
        //加载人才招聘列表
        private void LoadJob()
        {
            int psize = 10;
            int page = InputUtil.Get("page", 0);
            BLL.job bll = new BLL.job();
            string strWhere = "is_lock=0 and lang='en'";
            List<Model.job> jobList = bll.DataTableToList(bll.GetPageList(psize, page, strWhere, "sort_id asc,id desc").Tables[0]);
            int rcount = bll.GetRecordCount(strWhere);
            string pager = HtmlHelper.OutPageListEn(psize, page, rcount, "index.aspx?p=plug&act=job&page=__id__&name=" + Request["name"]);

            th.Put("jobList", jobList);
            th.Put("pager", pager);
        }
        //加载人才招聘详情
        private void LoadJobDetail()
        {
            int id = InputUtil.Get("id", 0);
            Model.job model = new BLL.job().GetModel(id);
            th.Put("model", model);
        }
        //加载职位申请
        private void LoadJobApply()
        {
            string jobTitle = InputUtil.Get("supply");

            th.Put("jobTitle", jobTitle);
        }
        //加载在线订单
        private void LoadOrder()
        {

        }
        //加载友情链接
        private void LoadLinks()
        {
            int psize = 12;
            int page = InputUtil.Get("page", 0);
            BLL.links bll = new BLL.links();
            string strWhere = "lang='en' and pos_mark='friend'";
            List<Model.links> linksList = bll.DataTableToList(bll.GetPageList(psize, page, strWhere, "sort_id asc,id desc").Tables[0]);
            int rcount = bll.GetRecordCount(strWhere);
            string pager = HtmlHelper.OutPageListEn(psize, page, rcount, "index.aspx?p=plug&act=links&page=__id__");

            th.Put("linksList", linksList);
            th.Put("pager", pager);
        }
        //加载物流跟踪
        private void LoadLogistics()
        {
            string orderNumber = InputUtil.Get("orderNumber");
            List<Model.logistics_status> list = new BLL.logistics_status().GetModelList("order_number='" + orderNumber + "'");
            th.Put("list", list);
        }
        //加载在线客服
        private void LoadService()
        {
            bool showqq = false;
            List<Model.olservice_group> serviceGroups = new List<Model.olservice_group>();
            if (siteConfig.olservice_status == (int)Enums.Status.开启)
            {
                showqq = true;
                serviceGroups = new BLL.olservice_group().GetModelList("is_lock=0 order by sort_id asc");
            }

            th.Put("showqq", showqq);
            th.Put("gerenqq", Enums.OlserviceType.个人QQ.ToString());
            th.Put("qiyeqq", Enums.OlserviceType.企业QQ.ToString());
            th.Put("qqqun", Enums.OlserviceType.QQ群.ToString());
            th.Put("aliwangwang", Enums.OlserviceType.阿里旺旺.ToString());
            th.Put("skype", Enums.OlserviceType.skype.ToString());
            th.Put("serviceGroups", serviceGroups);
        }
        //加载公共部分
        private void PutCommon()
        {
            string lockWindow = "";
            if (siteConfig.right_click_status == (int)Enums.Status.关闭)
            {
                lockWindow = "oncontextmenu=\"event.returnValue=false\" onselectstart =\"return false\"";
            }
            LoadService();

            //公共变量
            th.Put("page", InputUtil.Get("p", "index"));
            th.Put("lockWindow", lockWindow);
            th.Put("theme", theme);
            th.Put("info", siteInfo);
            th.Put("config", siteConfig);
            //公共类
            th.Put("this", this);
            th.Put("string", new StringHelper());
            th.Put("html", new HtmlHelper());
            th.Put("biz", new BizHelper());
        }
        //加载上下页
        private void PutPrevNext(int cid, int id)
        {
            BLL.article abll = new BLL.article();
            string prevHtml = "None";
            string nextHtml = "None";

            int prevId = abll.GetPrevId(cid, id);
            int nextId = abll.GetNextId(cid, id);

            if (prevId > 0)
            {
                prevHtml = string.Format("<a href=\"show_{0}.aspx\">{1}</a>", prevId, abll.GetModel(prevId).title);
            }
            if (nextId > 0)
            {
                nextHtml = string.Format("<a href=\"show_{0}.aspx\">{1}</a>", nextId, abll.GetModel(nextId).title);
            }
            th.Put("prevId", prevId);
            th.Put("nextId", nextId);
            th.Put("prevHtml", prevHtml);
            th.Put("nextHtml", nextHtml);
        }
        private void Display(string templateFile)
        {
            string templatePath = templateDir + templateFile;
            HttpContext.Current.Response.Write(th.BuildString(templateFile));
        }
    }
}